package cn.zhz.customview.view.tiktok;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.view.animation.LinearInterpolator;

import java.util.Random;


/**
 * @project: customViewDemo
 * @author: ZhuangHZ
 * @createTime: 2019/6/10 11:25
 * @describe:
 * @version:
 */
public class MusicPath {

    private Paint pathPaint;
    private Paint testPaint;
    private Path path;
    private PathMeasure pathMeasure;
    private float[] pos = new float[2];
    private float[] tan = new float[2];
    private float val;
    private Bitmap scaledBitmap;
    private Bitmap bitmap;
    private ValueAnimator animator;
    private ValueAnimator alphaAnim;
    private int alpha;
    private AnimatorSet animatorSet;
    private Random random;
    private int tmpRotate;

    public MusicPath() {
        random=new Random();
        int tmp=random.nextInt(2);
        if(tmp==0){
            tmpRotate=60;
        }else{
            tmpRotate=-60;
        }
        animator = ValueAnimator.ofFloat(0f, 1f);
        animator.setDuration(4000);
        animator.setRepeatCount(-1);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                val = (float) animation.getAnimatedValue();
            }
        });

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationRepeat(Animator animation) {
                super.onAnimationRepeat(animation);
                int tmp=random.nextInt(2);
                if(tmp==0){
                    tmpRotate=60;
                }else{
                    tmpRotate=-60;
                }
            }
        });

        alphaAnim = ValueAnimator.ofInt(128,255,255,0);
        alphaAnim.setDuration(4000);
        alphaAnim.setRepeatCount(-1);
        alphaAnim.setInterpolator(new LinearInterpolator());
        alphaAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                alpha= (int) animation.getAnimatedValue();
            }
        });
        animatorSet=new AnimatorSet();
    }

    public void init(Path path, Bitmap bitmap) {
        this.path = path;
        this.bitmap = bitmap;
        initPaint();
        pathMeasure = new PathMeasure(path, false);


    }

    private void initPaint() {
        pathPaint = new Paint();
        pathPaint.setAntiAlias(true);
        testPaint = new Paint();
        testPaint.setStyle(Paint.Style.STROKE);
        testPaint.setStrokeWidth(3);
        testPaint.setAntiAlias(true);
        testPaint.setColor(Color.WHITE);
    }


    public void onDraw(Canvas canvas) {
        if (animator.isRunning()) {
            canvas.save();
            pathMeasure.getPosTan(pathMeasure.getLength() * val, pos, tan);
            scaledBitmap = Bitmap.createScaledBitmap(bitmap, (int) ((bitmap.getWidth() * val * 0.3) + bitmap.getWidth()*0.2), (int) (bitmap.getHeight() * val * 0.3+bitmap.getHeight()*0.2) + 1, true);
            pathPaint.setAlpha(alpha);
            canvas.rotate(tmpRotate * val, pos[0], pos[1]);
            canvas.drawBitmap(scaledBitmap, pos[0], pos[1], pathPaint);
            canvas.restore();
        }
//        canvas.drawPath(path, testPaint);
    }


    public void start(long delay){
        animatorSet.setStartDelay(delay);
        animatorSet.play(animator).with(alphaAnim);
        animatorSet.start();
    }
    public void start(){
        start(0);
    }



}
